package edu.westga.sketchit2009cs.fakeNetwork;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

/**
 * Creates an fakeNetwork object which pretends
 * to be a real network and captures data.
 * 
 * @author RJ Hill
 *
 */
public class FakeNetwork {

	private File theFile;
	private BufferedWriter output;
	private Calendar rightNow = Calendar.getInstance();
	private String thisDay = Integer.toString(rightNow.get(Calendar.DAY_OF_MONTH));
	private String thisMonth = Integer.toString(rightNow.get(Calendar.MONTH));
	private String thisYear = Integer.toString(rightNow.get(Calendar.YEAR));
	private String datePrefix = this.thisDay + this.thisMonth + this.thisYear;
	
	
	public FakeNetwork() {
		
	}
	
	/**
	 * Creates a connection log with the date appended to the end of it
	 * <p>
	 * Requires: nothing.
	 * <p>
	 * Ensures: nothing.
	 * @param data the data to append to the file
	 */
	public void connectToNetwork(String data) {
		String fileName = "connectLog" + datePrefix + ".txt";
		writeToFile(fileName, data);
	}
	
	/**
	 * Creates a send log with the date appended to the end of it
	 * <p>
	 * Requires: nothing.
	 * <p>
	 * Ensures: nothing.
	 * @param data the data to append to the file
	 */
	public void sendData(String data) {
		String fileName = "sendLog" + datePrefix + ".txt";
		writeToFile(fileName, data);
	}
	
	/**
	 * Creates a receive log with the date appended to the end of it
	 * <p>
	 * Requires: nothing.
	 * <p>
	 * Ensures: nothing.
	 * @param data the data to append to the file
	 */
	public void receiveData(String data) {
		String fileName = "recieveLog" + datePrefix + ".txt";
		writeToFile(fileName, data);
	}
	
	/**
	 * Appends the given data to the specified file,
	 * if the file does not exist, it is created.
	 * <p>
	 * Requires: fileName != null
	 * <p>
	 * Ensures: the data is appended to the end of the file.
	 * @param fileName
	 * @param dataToWrite
	 */
	private void writeToFile(String fileName, String dataToWrite) {
		this.theFile = new File(fileName);
		
		//use buffering, reading one line at a time
    	//FileReader always assumes default encoding is OK!
		try {
	        output = new BufferedWriter(new FileWriter(this.theFile, true));
	        output.write(dataToWrite);
	        output.close();
	    } catch (IOException e) {
	    	
	    }

	}
	
}
